package cn.keniykey.mc.landterritory.command.only;

import java.util.regex.Pattern;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import cn.keniykey.mc.landterritory.command.CommandUtil;
import cn.keniykey.mc.landterritory.config.LanguageManager;
import cn.keniykey.mc.landterritory.land.LandManager;
import cn.keniykey.mc.landterritory.land.api.Land;
import cn.keniykey.mc.landterritory.landview.LandInfoView;
import cn.keniykey.mc.landterritory.landview.LandMainView;
import cn.keniykey.mc.viewlibrary.view.api.AbFrameView;
import cn.keniykey.mc.viewlibrary.view.api.FrameViewBundle;
import cn.keniykey.mc.viewlibrary.view.api.FrameViewIntent;

public final class CommandLand implements CommandExecutor{

	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String arg, String[] args) {
		if(!(sender instanceof Player)) {
			CommandUtil.sendMustPlayer(sender);
			return true;
		}
		final Player player = (Player) sender;
		player.closeInventory();
		if(args.length > 0) {
			final Land land; //领地详情
			final int lang;
			if (Pattern.matches("^\\d+$", args[0])) {
				lang = 1202000;
				land = LandManager.getLandManager().getLand(Long.valueOf(args[0]));
			}else {
				lang = 1202001;
				land = LandManager.getLandManager().getLand(args[0]);
			}
			if(land == null) {
				player.sendMessage(LanguageManager.getChatLang(lang, args[0]));
				return true;
			}
			final FrameViewBundle bundle = new FrameViewBundle();
			final FrameViewIntent intent = new FrameViewIntent(LandInfoView.class);
			bundle.putValue("land", land);
			intent.putExtras(bundle);
			AbFrameView.showView(player, intent);
			return true;
		}
		AbFrameView.showView(player, new FrameViewIntent(LandMainView.class));
		return true;
	}
}